﻿using MobiSms.DataModel;
using MobiSms.Enums;
using MobiSms.Library;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using MobiSms.Library.Extensions;

namespace MobiSms.ViewModel
{
    public class SerialAddViewModel : MainViewModel
    {
        public SerialAddViewModel() {
            SoapClient.BalanceCompleted -= SoapClient_BalanceCompleted;
            SoapClient.BalanceCompleted += SoapClient_BalanceCompleted;

            SerialItemToAdd.ValueChanged -= SerialItemToAdd_ValueChanged;
            SerialItemToAdd.ValueChanged += SerialItemToAdd_ValueChanged;
        }

        void SerialItemToAdd_ValueChanged(object sender, PropertyArgs e)
        {
            RaisePropertyChanged(() => BalanceCommand);
            RaisePropertyChanged(() => InsertCommand);
        }

        void SoapClient_BalanceCompleted(object sender, BalanceCompletedEventArgs e)
        {
            if (e.Result.code == 0)
            {
                SerialItemToAdd.Balance = Convert.ToInt64(e.Result.balance);
            }
            else
            {
                ShowResult<ResultCodeValue>(e.Result.code);
                //MessageBox.Show(((ResultCodeValue)e.Result.code).ToString());
            }
        }

        private SerialInfo _serialItemToAdd = new SerialInfo();
        public SerialInfo SerialItemToAdd
        {
            get { return _serialItemToAdd; }
            set
            {
                _serialItemToAdd = value;
                RaisePropertyChanged(() => SerialItemToAdd);
                SerialItemToAdd.ValueChanged -= SerialItemToAdd_ValueChanged;
                SerialItemToAdd.ValueChanged += SerialItemToAdd_ValueChanged;
            }
        }

        public ICommand BalanceCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return SerialItemToAdd.Available; },
                    ExecuteCallback = delegate
                    {
                        SoapClient.BalanceAsync(SerialItemToAdd.SerialNumber, SerialItemToAdd.Password);
                    }
                };
            }
        }

        public ICommand InsertCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return SerialItemToAdd.Available; },
                    ExecuteCallback = delegate
                    {
                        Database.CollectionOfSerialNumbers.InsertOnSubmit(SerialItemToAdd);
                        Submit();
                        MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DONE));
                        NavigateBack();
                    }
                };
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return true; },
                    ExecuteCallback = delegate
                    {
                        NavigateBack();
                    }
                };
            }
        }
    }
}
